﻿Imports System.Net
Imports System.Xml
Namespace bitly

    Public Class bitly

        Private our_login As String = ""
        Private our_apiKey As String = ""

        Private Const bly_format As String = "format=xml"
        Private Const bly_path As String = "http://api.bit.ly/v3/"

        ''' <summary>
        ''' Uses the defult, in-written API Login/keys
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()

        End Sub
        ''' <summary>
        ''' Allows different API login/keys to be used
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New(ByVal login As String, ByVal apiKey As String)
            our_login = login
            our_apiKey = apiKey
        End Sub

        ''' <summary>
        ''' Call bit.ly version 3 shorting API
        ''' </summary>
        ''' <returns>The shortened URL as a <c>bly_shorten_results</c>.</returns>
        ''' <remarks></remarks>
        Public Function shorten(ByVal ToShorten As bly_shorten_parameters) As bly_shorten_results
            Dim retVar As New bly_shorten_results
            Dim retFromBitly As String
            Dim wc As New WebClient
            Dim URL As String

            ' Build the inital part of the bit.ly reques URL
            URL = bly_path + "shorten?login=" + our_login + "&apiKey=" + our_apiKey + "&format=xml"

            If (ToShorten.Long_URL <> String.Empty) Then
                URL = URL + "&uri=" + ToShorten.Long_URL
            End If

            If (ToShorten.Domain <> String.Empty) Then
                URL = URL + "&domain=" + ToShorten.Domain
            End If

            If (ToShorten.x_login <> String.Empty) Then
                URL = URL + "&x_login=" + ToShorten.x_login
            End If

            If (ToShorten.x_apiKey <> String.Empty) Then
                URL = URL + "&x_apiKey=" + ToShorten.x_apiKey
            End If

            retFromBitly = wc.DownloadString(URL)

            Dim bitly_return As XDocument = XDocument.Parse(retFromBitly)


            If bitly_return.Root.Element("status_code").Value = "200" Then
                ' Status_code OK so lets pull the data into the retVar
                retVar.URL = bitly_return.Root.Element("data").Element("url").Value
                retVar.Hash = bitly_return.Root.Element("data").Element("hash").Value
                retVar.Global_Hash = bitly_return.Root.Element("data").Element("global_hash").Value
                retVar.Long_URL = bitly_return.Root.Element("data").Element("long_url").Value
                retVar.New_Hash = bitly_return.Root.Element("data").Element("new_hash").Value

                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            Else
                ' Status_code say no. Lets pull what we can
                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            End If

            Return retVar
        End Function

        ''' <summary>
        ''' Call bit.ly version 3 shorting API
        ''' </summary>
        ''' <returns>The shortened URL as a <c>String</c> to provide backward compatablity.</returns>
        ''' <remarks></remarks>
        Public Function shorten(ByVal ToShorten As String) As String
            Dim retVar As New bly_shorten_results
            Dim inVar As New bly_shorten_parameters

            inVar.Long_URL = ToShorten

            retVar = shorten(inVar)

            Return retVar.URL

        End Function

        ''' <summary>
        ''' Call bit.ly version 3 expanding API
        ''' </summary>
        ''' <returns>Returns the full URL for the link as a <c>string</c>.</returns>
        ''' <remarks></remarks>
        Public Function expand(ByVal ToExpand As String) As String
            Dim inVal As New bly_expand_parameters
            Dim retBly As New bly_expand_results
            Dim retVal As String

            inVal.Short_URL = ToExpand
            retBly = expand(inVal)
            retVal = retBly.Long_URL

            Return retVal

        End Function

        ''' <summary>
        ''' Call bit.ly version 3 expanding API
        ''' </summary>
        ''' <returns>The expanded URL as a <c>bly_expand_results</c>.</returns>
        ''' <remarks></remarks>
        Public Function expand(ByVal ToExpand As bly_expand_parameters) As bly_expand_results
            Dim retVar As New bly_expand_results
            Dim retFromBitly As String
            Dim wc As New WebClient
            Dim URL As String

            ' Build the inital part of the bit.ly reques URL
            URL = bly_path + "expand?login=" + our_login + "&apiKey=" + our_apiKey + "&format=xml"

            If (ToExpand.Hash <> String.Empty) Then
                URL = URL + "&hash=" + ToExpand.Hash
            End If

            If (ToExpand.Short_URL <> String.Empty) Then
                URL = URL + "&shortUrl=" + ToExpand.Short_URL
            End If

            retFromBitly = wc.DownloadString(URL)

            Dim bitly_return As XDocument = XDocument.Parse(retFromBitly)


            If bitly_return.Root.Element("status_code").Value = "200" Then
                ' Status_code OK so lets pull the data into the retVar
                retVar.Short_URL = bitly_return.Root.Element("data").Element("short_url").Value
                retVar.User_Hash = bitly_return.Root.Element("data").Element("user_hash").Value
                retVar.Global_hash = bitly_return.Root.Element("data").Element("global_hash").Value
                retVar.Long_URL = bitly_return.Root.Element("data").Element("long_url").Value

                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            Else
                ' Status_code say no. Lets pull what we can
                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            End If


            Return retVar
        End Function

        ''' <summary>
        ''' Call bit.ly version 3 clicks API
        ''' </summary>
        ''' <returns>The click data as <c>bly_clicks_results</c>.</returns>
        ''' <remarks></remarks>
        Public Function clicks(ByVal linkData As bly_clicks_parameters) As bly_clicks_results
            Dim retVar As New bly_clicks_results
            Dim retFromBitly As String
            Dim wc As New WebClient
            Dim URL As String

            ' Build the inital part of the bit.ly reques URL
            URL = bly_path + "expand?login=" + our_login + "&apiKey=" + our_apiKey + "&format=xml"

            If (linkData.Hash <> String.Empty) Then
                URL = URL + "&hash=" + linkData.Hash
            End If

            If (linkData.Short_URL <> String.Empty) Then
                URL = URL + "&shortUrl=" + linkData.Short_URL
            End If

            retFromBitly = wc.DownloadString(URL)

            Dim bitly_return As XDocument = XDocument.Parse(retFromBitly)


            If bitly_return.Root.Element("status_code").Value = "200" Then
                ' Status_code OK so lets pull the data into the retVar

                ' Some how I need to check if bitly_return.Root.Element("data").Element("error") exists, and if it does flag in the status as an error
                retVar.Short_URL = bitly_return.Root.Element("data").Element("short_url").Value
                retVar.User_hash = bitly_return.Root.Element("data").Element("user_hash").Value
                retVar.Global_hash = bitly_return.Root.Element("data").Element("global_hash").Value
                retVar.Global_clicks = bitly_return.Root.Element("data").Element("global_clicks").Value
                retVar.User_clicks = bitly_return.Root.Element("data").Element("user_clicks").Value

                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            Else
                ' Status_code say no. Lets pull what we can
                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            End If


            Return retVar
        End Function


        Public Function look(ByVal ParamArray URL() As String) As bly_look

            Dim URLtoCall As String
            Dim wc As New WebClient
            Dim retFromBitly As String
            Dim retVar As New bly_look



            ' Build the inital part of the bit.ly reques URL
            URLtoCall = bly_path + "shorten?login=" + our_login + "&apiKey=" + our_apiKey + "&format=xml"

            For Each URLtoAdd As String In URL
                URLtoCall = URLtoCall + "&url=" + URLtoAdd
            Next

            retFromBitly = wc.DownloadString(URLtoCall)


            Dim bitly_return As XDocument = XDocument.Parse(retFromBitly)

            If bitly_return.Root.Element("status_code").Value = "200" Then
                ' Status_code OK so lets pull the data into the retVar

                For Each node As XElement In bitly_return...<data>
                    Dim retVar_Inner As New bly_look_inner
                    retVar_Inner.Global_hash = node.<global_hash>.Value
                    retVar_Inner.Short_URL = node.<short_ur>.Value
                    retVar_Inner.URL = node.<url>.Value
                    retVar.Look.Add(retVar_Inner)
                Next


                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            Else
                ' Status_code say no. Lets pull what we can
                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            End If

            Return retVar

        End Function

        ''' <summary>
        ''' validate's if the x_login/x_apiKey data is valid
        ''' </summary>
        ''' <returns>The information on the login/key combo as <c>bly_validate_results</c>.</returns>
        ''' <remarks></remarks>
        Public Function validate(ByVal x_data As bly_validate_parameters) As bly_validate_results
            Dim retVar As New bly_validate_results
            Dim retFromBitly As String
            Dim wc As New WebClient
            Dim URL As String

            ' Build the inital part of the bit.ly reques URL
            URL = bly_path + "expand?login=" + our_login + "&apiKey=" + our_apiKey + "&format=xml"

            If (x_data.x_login <> String.Empty) Then
                URL = URL + "&x_login=" + x_data.x_login
            End If

            If (x_data.x_apiKey <> String.Empty) Then
                URL = URL + "&x_apiKey=" + x_data.x_apiKey
            End If


            retFromBitly = wc.DownloadString(URL)

            Dim bitly_return As XDocument = XDocument.Parse(retFromBitly)


            If bitly_return.Root.Element("status_code").Value = "200" Then
                ' Status_code OK so lets pull the data into the retVar

                retVar.Valid = bitly_return.Root.Element("data").Element("valid").Value

                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            Else
                ' Status_code say no. Lets pull what we can
                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            End If


            Return retVar
        End Function

        ''' <summary>
        ''' validate's if the pro domain is really a bit.ly pro account
        ''' </summary>
        ''' <returns>The information on the pro domain as <c>bly_pro_domain_results</c>.</returns>
        ''' <remarks></remarks>
        Public Function pro_domain(ByVal domainData As bly_pro_domain_parameters) As bly_pro_domain_results
            Dim retVar As New bly_pro_domain_results
            Dim retFromBitly As String
            Dim wc As New WebClient
            Dim URL As String

            ' Build the inital part of the bit.ly reques URL
            URL = bly_path + "expand?login=" + our_login + "&apiKey=" + our_apiKey + "&format=xml"

            If (domainData.Domain <> String.Empty) Then
                URL = URL + "&domain=" + domainData.Domain
            End If

            retFromBitly = wc.DownloadString(URL)

            Dim bitly_return As XDocument = XDocument.Parse(retFromBitly)


            If bitly_return.Root.Element("status_code").Value = "200" Then
                ' Status_code OK so lets pull the data into the retVar

                retVar.Bitly_pro_domain = bitly_return.Root.Element("data").Element("bitly_pro_domain").Value
                retVar.Domain = bitly_return.Root.Element("data").Element("domain").Value

                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            Else
                ' Status_code say no. Lets pull what we can
                retVar.Status.Status_Code = bitly_return.Root.Element("status_code").Value
                retVar.Status.Status_txt = bitly_return.Root.Element("status_txt").Value
            End If


            Return retVar
        End Function

    End Class

End Namespace

